package com.scoy.honeymei.activity.home;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.adapter.CheckTicketAdapter;
import com.scoy.honeymei.base.BaseActivity;
import com.scoy.honeymei.bean.CouponBean;
import com.scoy.honeymei.bean.DateBean;
import com.scoy.honeymei.bean.TravelSettleBean;
import com.scoy.honeymei.calendar.ClickDataListener;
import com.scoy.honeymei.calendar.MyCalendarView;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.CheckUtil;
import com.scoy.honeymei.utils.IntentUtil;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;
import com.scoy.honeymei.utils.SystemUtil;
import com.scoy.honeymei.utils.TimeUtil;
import com.scoy.honeymei.wxapi.PayEvent;
import com.scoy.honeymei.wxapi.PayUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: TravelSettleActivity$
 * @Description: 旅游结算界面
 * @Author: GaoZhan
 * @CreateDate: 2019/9/6/0006$ 下午 16:15$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/9/6/0006$ 下午 16:15$
 * @UpdateRemark: 更新内容
 */
public class TravelSettleActivity extends BaseActivity {

    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.sign_tv)
    TextView signTv;
    @BindView(R.id.title_llt)
    LinearLayout titleLlt;
    @BindView(R.id.sst_startadd_tv)
    TextView sstStartaddTv;
    @BindView(R.id.date0_tv)
    TextView date0Tv;
    @BindView(R.id.sign0_tv)
    TextView sign0Tv;
    @BindView(R.id.price0_tv)
    TextView price0Tv;
    @BindView(R.id.dtime0_cl)
    ConstraintLayout dtime0Cl;
    @BindView(R.id.date1_tv)
    TextView date1Tv;
    @BindView(R.id.sign1_tv)
    TextView sign1Tv;
    @BindView(R.id.price1_tv)
    TextView price1Tv;
    @BindView(R.id.dtime1_cl)
    ConstraintLayout dtime1Cl;
    @BindView(R.id.date2_tv)
    TextView date2Tv;
    @BindView(R.id.sign2_tv)
    TextView sign2Tv;
    @BindView(R.id.price2_tv)
    TextView price2Tv;
    @BindView(R.id.dtime2_cl)
    ConstraintLayout dtime2Cl;
    @BindView(R.id.sst_more_tv)
    TextView sstMoreTv;
    @BindView(R.id.tst_manprice_tv)
    TextView tstManpriceTv;
    @BindView(R.id.ct_subtract_tv)
    TextView ctSubtractTv;
    @BindView(R.id.ct_num_tv)
    TextView ctNumTv;
    @BindView(R.id.ct_add_tv)
    TextView ctAddTv;
    @BindView(R.id.tst_childprice_tv)
    TextView tstChildpriceTv;
    @BindView(R.id.ct_subtract1_tv)
    TextView ctSubtract1Tv;
    @BindView(R.id.ct_num1_tv)
    TextView ctNum1Tv;
    @BindView(R.id.ct_add1_tv)
    TextView ctAdd1Tv;
    @BindView(R.id.sst_name_et)
    EditText sstNameEt;
    @BindView(R.id.sst_phone_et)
    EditText sstPhoneEt;
    @BindView(R.id.sst_checkticket_tv)
    TextView sstCheckticketTv;
    @BindView(R.id.sst_price_tv)
    TextView sstPriceTv;
    @BindView(R.id.sst_totalprice_tv)
    TextView sstTotalpriceTv;
    @BindView(R.id.tst_price_tv)
    TextView tstPriceTv;
    @BindView(R.id.tst_buy_tv)
    TextView tstBuyTv;
    private TravelSettleActivity mContext;
    private Dialog dialogtr;
    private int trId;
    private TravelSettleBean.FirstDayBean first_day;
    private TravelSettleBean.SecondDayBean second_day;
    private TravelSettleBean.ThreeDayBean three_day;
    private String currentManPrice;
    private String currentChildPrice;
    private String payType = "null";

    private int couponId;
    private String selectTime;
    private ArrayList<CouponBean> datacklist;
    private double shouldNum;
    private double couponNum;
    private int checkedPosition = -1;
    private String payThree;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settle_travel);
        ButterKnife.bind(this);
        mContext = TravelSettleActivity.this;
        trId = getIntent().getIntExtra("trId", 0);
        String trTime = getIntent().getStringExtra("trTime");

        initNormal();

        httpTravelgetSettle(trTime);
    }

    @Override
    public void initNormal() {
        datacklist = new ArrayList<>();

        titleTv.setText(R.string.Settlement);
        MyUtil.setStatusBar(mContext, getWindow(), true, R.color.colorWhite);
        titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);
        signTv.setText("分享");

        EventBus.getDefault().register(this);

    }

    @OnClick({R.id.back_iv, R.id.dtime0_cl, R.id.dtime1_cl, R.id.dtime2_cl, R.id.ct_subtract_tv, R.id.ct_add_tv,
            R.id.ct_subtract1_tv, R.id.ct_add1_tv, R.id.sst_checkticket_tv, R.id.tst_buy_tv, R.id.sst_more_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.dtime0_cl:
                if (dtime0Cl.isSelected()) {
                    return;
                }
                dtime0Cl.setSelected(true);
                dtime1Cl.setSelected(false);
                dtime2Cl.setSelected(false);
                currentManPrice = first_day.getPrice();
                currentChildPrice = first_day.getChild_price();
                tstManpriceTv.setText(first_day.getPrice() + "/人");
                tstChildpriceTv.setText(first_day.getChild_price() + "/人");
                selectTime = first_day.getTime();
                setMoney();
                break;
            case R.id.dtime1_cl:
                if (dtime1Cl.isSelected()) {
                    return;
                }
                dtime0Cl.setSelected(false);
                dtime1Cl.setSelected(true);
                dtime2Cl.setSelected(false);
                currentManPrice = second_day.getPrice();
                currentChildPrice = second_day.getChild_price();
                tstManpriceTv.setText(second_day.getPrice() + "/人");
                tstChildpriceTv.setText(second_day.getChild_price() + "/人");
                selectTime = second_day.getTime();
                setMoney();
                break;
            case R.id.dtime2_cl:
                if (dtime2Cl.isSelected()) {
                    return;
                }
                dtime0Cl.setSelected(false);
                dtime1Cl.setSelected(false);
                dtime2Cl.setSelected(true);
                currentManPrice = three_day.getPrice();
                currentChildPrice = three_day.getChild_price();
                tstManpriceTv.setText(three_day.getPrice() + "/人");
                tstChildpriceTv.setText(three_day.getChild_price() + "/人");
                selectTime = three_day.getTime();
                setMoney();
                break;
            case R.id.sst_more_tv:
                httpCalender();

                break;
            case R.id.ct_subtract_tv:
                int i0 = Integer.parseInt(ctNumTv.getText().toString());
                if (i0 > 0) {
                    ctNumTv.setText(i0 - 1 + "");
                    setMoney();
                }
                break;
            case R.id.ct_add_tv:
                int i00 = Integer.parseInt(ctNumTv.getText().toString());
                ctNumTv.setText(i00 + 1 + "");
                setMoney();

                break;
            case R.id.ct_subtract1_tv:
                int i01 = Integer.parseInt(ctNum1Tv.getText().toString());
                if (i01 > 0) {
                    ctNum1Tv.setText(i01 - 1 + "");
                    setMoney();
                }
                break;
            case R.id.ct_add1_tv:
                int i02 = Integer.parseInt(ctNum1Tv.getText().toString());
                ctNum1Tv.setText(i02 + 1 + "");
                setMoney();
                break;
            case R.id.sst_checkticket_tv:
                onCheck();
                break;
            case R.id.tst_buy_tv:
                if ("NULL".equals(selectTime)) {
                    return;
                }
                if (Integer.parseInt(ctNumTv.getText().toString()) + Integer.parseInt(ctNum1Tv.getText().toString()) < 1) {
                    MyUtil.mytoast(mContext, "请选择人数");
                    return;
                }
                if ((Double.parseDouble(currentManPrice) * Integer.parseInt(ctNumTv.getText().toString()) + Double.parseDouble(currentChildPrice) * Integer.parseInt(ctNum1Tv.getText().toString())) < shouldNum) {
                    MyUtil.mytoast(mContext, "优惠券未达到使用要求，无法使用");
                    return;
                }
                if (TextUtils.isEmpty(sstNameEt.getText())) {
                    MyUtil.mytoast(mContext, "请输入取票人姓名");
                    return;
                }
                if (TextUtils.isEmpty(sstPhoneEt.getText())) {
                    MyUtil.mytoast(mContext, "请输入取票人电话");
                    return;
                }
                if (!CheckUtil.isMobileNO(sstPhoneEt.getText().toString())) {
                    MyUtil.mytoast(mContext, "请输入正确的电话");
                    return;
                }
                showdialog();

                break;
        }
    }


    public void onCalender(ArrayList<DateBean> datelist) {
        dialogtr = new Dialog(mContext, R.style.BottomDialogStyle);
        View view = View.inflate(mContext, R.layout.dialog_calender, null);
        ImageView closeIv = view.findViewById(R.id.mcv_close_iv);
        MyCalendarView mcv = view.findViewById(R.id.mcv_cv);
        mcv.setSign(datelist);
        mcv.setClickDataListener(new ClickDataListener() {
            @Override
            public void clickData(int year, int month, int day) {
                dialogtr.dismiss();
                httpTravelgetSettle(year + "-" + MyUtil.numTo2String(month) + "-" + MyUtil.numTo2String(day));
            }
        });

        dialogtr.setContentView(view);
        dialogtr.setCanceledOnTouchOutside(true);
        Window dialogWindow = dialogtr.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (SystemUtil.getScreenWidth(mContext));
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        dialogWindow.setAttributes(lp);
        closeIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogtr.dismiss();

            }
        });

        dialogtr.show();
    }

    //预约
    private void httpTravelgetSettle(String cDate) {
        HttpParams params = new HttpParams();
        params.put("attractions_id", trId);
        params.put("this_time", cDate);//2019-11-10
        params.put("uid", SPHelper.getString(MeConstant.UID, ""));
        HpGo.newInstance().HttpGo(this, MyUrl.TRAVEL_BUY_NOW, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                TravelSettleBean trBean = new Gson().fromJson(o, TravelSettleBean.class);
                payThree = trBean.getPaytype();

                TravelSettleBean.AttractionInfoBean attraction_info = trBean.getAttraction_info();
                sstStartaddTv.setText(attraction_info.getName());

                first_day = trBean.getFirst_day();
                date0Tv.setText(first_day.getTime());
                price0Tv.setText(first_day.getPrice());

                second_day = trBean.getSecond_day();
                date1Tv.setText(second_day.getTime());
                price1Tv.setText(second_day.getPrice());

                three_day = trBean.getThree_day();
                date2Tv.setText(three_day.getTime());
                price2Tv.setText(three_day.getPrice());

                if (first_day.getPrice() == null) {
                    dtime0Cl.setSelected(false);
                    dtime0Cl.setVisibility(View.GONE);
                } else {
                    dtime0Cl.setSelected(true);
                    dtime0Cl.setVisibility(View.VISIBLE);
                }

                if (second_day.getPrice() == null) {
                    dtime1Cl.setSelected(false);
                    dtime1Cl.setVisibility(View.GONE);
                } else {
                    dtime1Cl.setVisibility(View.VISIBLE);
                    if (dtime0Cl.isSelected()) {
                        dtime1Cl.setSelected(false);
                    } else {
                        dtime1Cl.setSelected(true);
                    }
                }

                if (three_day.getPrice() == null) {
                    dtime2Cl.setSelected(false);
                    dtime2Cl.setVisibility(View.GONE);
                } else {
                    dtime2Cl.setVisibility(View.VISIBLE);
                    if (!dtime0Cl.isSelected() && !dtime1Cl.isSelected()) {
                        dtime2Cl.setSelected(true);
                    } else {
                        dtime2Cl.setSelected(false);
                    }
                }

                if (dtime0Cl.isSelected() || dtime1Cl.isSelected() || dtime2Cl.isSelected()) {
                    if (dtime0Cl.isSelected()) {
                        currentManPrice = first_day.getPrice();
                        currentChildPrice = first_day.getChild_price();
                        tstManpriceTv.setText(first_day.getPrice() + "/人");
                        tstChildpriceTv.setText(first_day.getChild_price() + "/人");
                        selectTime = first_day.getTime();
                    } else if (dtime1Cl.isSelected()) {
                        currentManPrice = second_day.getPrice();
                        currentChildPrice = second_day.getChild_price();
                        tstManpriceTv.setText(second_day.getPrice() + "/人");
                        tstChildpriceTv.setText(second_day.getChild_price() + "/人");
                        selectTime = second_day.getTime();
                    } else if (dtime2Cl.isSelected()) {
                        currentManPrice = three_day.getPrice();
                        currentChildPrice = three_day.getChild_price();
                        tstManpriceTv.setText(three_day.getPrice() + "/人");
                        tstChildpriceTv.setText(three_day.getChild_price() + "/人");
                        selectTime = three_day.getTime();
                    }

                } else {
                    currentManPrice = "0.0";
                    currentChildPrice = "0.0";
                    tstManpriceTv.setText("0.0/人");
                    tstChildpriceTv.setText("0.0/人");
                    selectTime = "NULL";
                }


                if (datacklist != null) {
                    datacklist.clear();
                }
                List<CouponBean> coupon_list = trBean.getCoupon_list();
                if (coupon_list != null && coupon_list.size() > 0) {
                    sstCheckticketTv.setText("有可用优惠券");
                    datacklist.addAll(coupon_list);
                } else {
                    sstCheckticketTv.setText("无可用优惠券");
                    sstCheckticketTv.setEnabled(false);
                }
                couponId = 0;
                couponNum = 0.0;
                shouldNum = 0.0;


                setMoney();
            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }

    private void setMoney() {
        String manPrice = currentManPrice;
        String childPrice = currentChildPrice;
        if (manPrice == null || childPrice == null) {
            sstPriceTv.setText("￥" + 0.00);
            sstTotalpriceTv.setText("￥" + 0.00);
            tstPriceTv.setText("￥" + 0.00);
            return;
        }
        sstPriceTv.setText("￥" + manPrice);

        double vpAll = Double.parseDouble(manPrice) * Integer.parseInt(ctNumTv.getText().toString())
                + Double.parseDouble(childPrice) * Integer.parseInt(ctNum1Tv.getText().toString())
                - couponNum;
        String sall = MyUtil.doubleTo2String(vpAll < 0 ? 0.0 : vpAll);
        sstTotalpriceTv.setText("￥" + sall);
        tstPriceTv.setText(sall);
    }

    /**
     * 选择弹窗
     */
    private void showdialog() {
        final Dialog dialogpay = new Dialog(this, R.style.BottomDialogStyle);
        View view = View.inflate(this, R.layout.dialog_tocheckpay, null);

        ImageView imgClose = view.findViewById(R.id.imgClose);
        Button btnSure = view.findViewById(R.id.btnSure);
        TextView alipayTv = view.findViewById(R.id.txtZhifubao);
        TextView wxpayTv = view.findViewById(R.id.txtWechat);
        //     TextView walletTv = view.findViewById(R.id.txtWallet);

        dialogpay.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                payType = "null";
            }
        });

        imgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialogpay.dismiss();
            }
        });
        alipayTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alipayTv.setSelected(true);
                wxpayTv.setSelected(false);
                //    walletTv.setSelected(false);
                payType = "alipay";
            }
        });
        wxpayTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alipayTv.setSelected(false);
                wxpayTv.setSelected(true);
                //  walletTv.setSelected(false);
                payType = "wechat";
            }
        });


        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!"null".equals(payType)) {
                    payOrder(payType);
                    dialogpay.dismiss();
                }
            }
        });

        dialogpay.setContentView(view);
        dialogpay.setCanceledOnTouchOutside(true);
        Window dialogWindow = dialogpay.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (SystemUtil.getScreenWidth(mContext));
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        dialogWindow.setAttributes(lp);
        dialogpay.show();

    }

    //旅游支付
    private void payOrder(String payType) {
        HttpParams params = new HttpParams();
        params.put("uid", SPHelper.getString(MeConstant.UID, ""));
        params.put("type", payType);

        params.put("name", sstNameEt.getText().toString());
        params.put("mobile", sstPhoneEt.getText().toString());
        params.put("coupon_id", couponId);
        params.put("attractions_id", trId);
        params.put("num", ctNumTv.getText().toString());
        params.put("childnum", ctNum1Tv.getText().toString());
        params.put("times", selectTime);

        HpGo.newInstance().HttpGo(mContext, MyUrl.TRAVEL_TOPAY, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                // payThree  1  三方； 2  原生
                if ("2".equals(payThree)) {
                    if ("false".equals(o)) {
                        MyUtil.mytoast(mContext, "支付错误");
                        return;
                    }
                    PayUtil.toPayPayPay(mContext, payType, o);//支付
                } else {
                    if ("alipay".equals(payType)) {
                        IntentUtil.openAPP(mContext, MyUrl.APP_OPEN_ALIPAY + o);
                        finish();
                    } else {
                        if ("false".equals(o)) {
                            MyUtil.mytoast(mContext, "支付错误");
                            return;
                        }
                        PayUtil.toPayPayPay(mContext, payType, o);//支付
                        //  IntentUtil.openAPP(mContext, MyUrl.APP_OPEN_WX + o);
                    }

                }
            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }

    /**
     * 选择优惠券弹窗
     */
    public void onCheck() {
        final Dialog dialogcheck = new Dialog(mContext, R.style.BottomDialogStyle);
        View view = View.inflate(mContext, R.layout.dialog_check_ticket, null);
        TextView cancleTv = view.findViewById(R.id.cts_cancle_tv);
        TextView sureTv = view.findViewById(R.id.cts_sure_tv);
        RecyclerView dialogRv = view.findViewById(R.id.cts_rv);


        CheckTicketAdapter checkTicketAdapter = new CheckTicketAdapter(mContext, datacklist);
        dialogRv.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        DividerItemDecoration did = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
        did.setDrawable(getResources().getDrawable(R.drawable.divider_gray_line_pd));
        dialogRv.setAdapter(checkTicketAdapter);
        if (checkedPosition != -1) {
            checkTicketAdapter.setCheckedPosition(checkedPosition);
        }

        dialogcheck.setContentView(view);
        dialogcheck.setCanceledOnTouchOutside(true);
        Window dialogWindow = dialogcheck.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (SystemUtil.getScreenWidth(mContext));
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        dialogWindow.setAttributes(lp);
        cancleTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogcheck.dismiss();

            }
        });

        sureTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogcheck.dismiss();
                checkedPosition = checkTicketAdapter.getCheckedPosition();
                if (checkedPosition != -1) {
                    CouponBean couponBean = datacklist.get(checkedPosition);
                    couponId = couponBean.getId();
                    couponNum = Double.parseDouble(couponBean.getMoney());
                    shouldNum = Double.parseDouble(couponBean.getCondition());
                    sstCheckticketTv.setText(couponBean.getType_name() + "  满" + couponBean.getCondition() + "元优惠" + couponBean.getMoney() + "元");
                } else {
                    couponId = 0;
                    couponNum = 0.0;
                    shouldNum = 0.0;
                    sstCheckticketTv.setText("不使用优惠券");
                }

                setMoney();
            }
        });

        dialogcheck.show();
    }

    /**
     * 日历
     */
    private void httpCalender() {
        HttpParams params = new HttpParams();
        params.put("id", trId);
        HpGo.newInstance().HttpGo(this, MyUrl.TRAVEL_CALENDAR, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                ArrayList<DateBean> datelist = new Gson().fromJson(o, new TypeToken<ArrayList<DateBean>>() {
                }.getType());
                onCalender(datelist);
            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onPayEvent(PayEvent event) {
        int type = event.getType();//0 成功 -1失败 -2取消
        if (type == 0) {
            MyUtil.mytoast0(mContext, "支付成功", R.mipmap.ic_sure);
            setResult(0x871);
            finish();
        } else if (type == -1) {
            MyUtil.mytoast(mContext, "支付失败");
        } else {
            MyUtil.mytoast(mContext, "支付取消");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
